package com.sdygmedia.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sdygmedia.dao.ArticleDao;
import com.sdygmedia.domain.Admin;
import com.sdygmedia.domain.Article;
import com.sdygmedia.domain.Category;
import com.sdygmedia.domain.CategoryChild;
import com.sdygmedia.service.ArticleService;
import com.sdygmedia.service.CategoryChildService;

@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {

	@Resource
	ArticleDao articleDao;
	@Resource
	CategoryChildService categoryChildService;
	
	@Override
	public List<Article> findAll() {
		// TODO Auto-generated method stub
		return articleDao.findAll();
	}

	@Override
	public void delete(long id) {
		// TODO Auto-generated method stub
		articleDao.delete(id);
	}

	@Override
	public void save(Article article) {
		// TODO Auto-generated method stub
		
		//通过content获取 里边的图片地址和 简单的文章描述
		String content = article.getContent();
		
		int beginIndex  = content.indexOf("<img src=");
		int endIndex = content.indexOf(" alt=", beginIndex );
		
		String imgUrl = content.substring(beginIndex+10 , endIndex-1);
		String description = content.substring(endIndex+9, endIndex+49);
		
		article.setImgUrl(imgUrl);
		article.setDescription(description);
		
		article.setDate(new Date());
		
		article.setCategoryChild(categoryChildService.getById(article.getCategoryChild().getId()));
		article.setCategory(article.getCategoryChild().getCategory());
		
		articleDao.save(article);
	}

	@Override
	public Article getById(long id) {
		// TODO Auto-generated method stub
		return articleDao.getById(id);
	}

	@Override
	public void update(Article article) {
		// TODO Auto-generated method stub
		article.setDate(new Date());
		articleDao.update(article);
	}

	@Override
	public List<Article> getByCategory(Category category) {
		// TODO Auto-generated method stub
		return articleDao.getByCategory(category);
	}

	@Override
	public List<Article> getByCategoryChild(CategoryChild categoryChild) {
		// TODO Auto-generated method stub
		return articleDao.getByCategoryChild(categoryChild);
	}

	@Override
	public List getIndexImage() {
		// TODO Auto-generated method stub
		return articleDao.getIndexImage();
	}

	

}
